<template>
  <div class="container">
    <div class="wrapper">
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span style="font-size: 30px; color: #4ab344">个人中心</span>
        </div>
        <el-tabs v-model="activeTab" tab-position="left">
          <el-tab-pane label="基本资料" name="userinfo">
            <el-form ref="form" :model="user" :rules="rules" label-width="120px">
              <el-form-item prop="name" label="姓名">
                <el-input v-model="user.name" type="text" auto-complete="off" placeholder="请输入姓名" disabled>
                </el-input>
              </el-form-item>
              <el-form-item prop="sex" label="性别">
                <el-radio v-model="user.sex" label="0"  disabled>男</el-radio>
                <el-radio v-model="user.sex" label="1"  disabled>女</el-radio>
              </el-form-item>
              <el-form-item prop="phone" label="手机号">
                <el-input v-model="user.phone" type="text" auto-complete="off" placeholder="请输入手机号">
                </el-input>
              </el-form-item>
              <el-form-item prop="idCard" label="身份证号码">
                <el-input v-model="user.idCard" type="text" auto-complete="off" placeholder="请输入身份证号码" >
                </el-input>
              </el-form-item>
              <el-form-item prop="email" label="电子邮箱">
                <el-input v-model="user.email" type="text" auto-complete="off" placeholder="请输入电子邮箱">
                </el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" size="mini" @click="submit"
                >保存
                </el-button
                >
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="修改密码" name="resetPwd">
            <el-form
                ref="pwdForm"
                :model="pwdUser"
                :rules="pwdRules"
                label-width="80px"
            >
              <el-form-item label="旧密码" prop="password">
                <el-input
                    v-model="pwdUser.password"
                    placeholder="请输入旧密码"
                    type="password"
                    show-password
                />
              </el-form-item>
              <el-form-item label="新密码" prop="newPassWord">
                <el-input
                    v-model="pwdUser.newPassWord"
                    placeholder="请输入新密码"
                    type="password"
                    show-password
                />
              </el-form-item>
              <el-form-item label="确认密码" prop="confirmPassword">
                <el-input
                    v-model="pwdUser.confirmPassword"
                    placeholder="请确认密码"
                    type="password"
                    show-password
                />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" size="mini" @click="submit2"
                >保存
                </el-button
                >
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="客房订单" name="hotelInfo">
            <el-table :data="roomData" style="width: 100%" stripe>
              <el-table-column prop="room.name" label="房间" width="150">
              </el-table-column>
              <el-table-column prop="liveTime" label="入住时间" width="180">
              </el-table-column>
              <el-table-column prop="leaveTime" label="离店时间" width="180">
              </el-table-column>
              <el-table-column prop="orderPrice" label="订单价格(元)"> </el-table-column>
              <el-table-column prop="statusName" label="订单状态"> </el-table-column>
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button
                      size="mini"
                      type="success"
                      v-if="scope.row.status==1"
                      @click="handleCancel(scope.row)"
                  >取消
                  </el-button>
                  <el-button
                      size="mini"
                      type="danger"
                      v-if="scope.row.status==3"
                      @click="handleDelete(scope.row)"
                  >删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
            <div class="page">
              <el-pagination
                  @size-change="handleSizeChange"
                  @current-change="handleCurrentChange"
                  :current-page="roomQuery.pageNum"
                  :page-sizes="[10]"
                  :page-size="roomQuery.pageSize"
                  layout="total, sizes, prev, pager, next"
                  :total="roomQuery.total"
              >
              </el-pagination>
            </div>
          </el-tab-pane>
          <el-tab-pane label="餐饮订单" name="foodInfo">
            <el-table :data="restaurantData" style="width: 100%" stripe>
              <el-table-column prop="res" label="餐饮" width="205">
              </el-table-column>
              <el-table-column prop="orderPrice" label="订单价格(元)" width="200">
              </el-table-column>
              <el-table-column prop="statusName" label="订单状态" width="200">
              </el-table-column>
              <el-table-column prop="createTime" label="预订时间"> </el-table-column>
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button
                      size="mini"
                      type="success"
                      v-if="scope.row.status==1"
                      @click="handleCancel2(scope.row)"
                  >取消
                  </el-button>
                  <el-button
                      size="mini"
                      type="danger"
                      v-if="scope.row.status==3"
                      @click="handleDelete2(scope.row)"
                  >删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
            <div class="page">
              <el-pagination
                  @size-change="handleSizeChange2"
                  @current-change="handleCurrentChange2"
                  :current-page="restaurantQuery.pageNum"
                  :page-sizes="[10]"
                  :page-size="restaurantQuery.pageSize"
                  layout="total, sizes, prev, pager, next"
                  :total="restaurantQuery.total"
              >
              </el-pagination>
            </div>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import {
  getTourist,
  updateTourist,
  updateTouristPwd,
  getRestaurantOrder,
  getRoomOrder,
  delRoomOrder,
  delRestaurantOrder,
  cancelRoomOrder,
  cancelRestaurantOrder
} from "@/utils/request";
import {Message} from "element-ui";

export default {
  name: "Person",
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.pwdUser.newPassWord !== value) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    const checkPhone = (rule, value, callback) => {
      const reg = /^1(?:3\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\d|9\d)\d{8}$/;
      if (!value) {
        return callback(new Error('请输入您的手机号'));
      }
      setTimeout(() => {
        if (!reg.test(value)) {
          callback(new Error('请输入正确的手机号'));
        } else {
          callback();
        }
      }, 500);
    };
    const checkEmail = (rule, value, callback) => {
      const reg = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+)+$/;
      if (!value) {
        return callback(new Error('请输入您的邮箱'));
      }
      setTimeout(() => {
        if (!reg.test(value)) {
          callback(new Error('请输入正确的邮箱'));
        } else {
          callback();
        }
      }, 500);
    };
    const checkIdCard = (rule, value, callback) => {
      const reg = /^([1-6][1-9]|50)\d{4}(18|19|20)\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
      if (!value) {
        return callback(new Error('请输入您的身份证号码'));
      }
      setTimeout(() => {
        if (!reg.test(value)) {
          callback(new Error('请输入正确的身份证号码'));
        } else {
          callback();
        }
      }, 500);
    };
    return {
      roomData: [],
      restaurantData: [],
      user: {
        name: "",
        phone: "",
        idCard: "",
        sex: "",
        email: "",
      },
      roomQuery: {
        pageSize: 10,
        pageNum: 1,
        total: 0,
      },
      restaurantQuery: {
        pageSize: 10,
        pageNum: 1,
        total: 0,
      },
      activeTab: "userinfo",
      // 表单校验
      rules: {
        name: [
          {required: true, trigger: "blur", message: "请输入您的姓名"}
        ],
        sex: [
          {required: true, trigger: "blur", message: "请选择您的性别"}
        ],
        idCard: [
          {required: true, validator: checkIdCard, trigger: 'blur'}
        ],
        phone: [
          {required: true, validator: checkPhone, trigger: 'blur'}
        ],
        email: [
          {required: false, validator: checkEmail, trigger: 'blur'}
        ],
      },
      pwdUser: {},
      // 表单校验
      pwdRules: {
        password: [
          {required: true, message: "旧密码不能为空", trigger: "blur"},
        ],
        newPassWord: [
          {required: true, message: "新密码不能为空", trigger: "blur"},
          {
            min: 6,
            max: 20,
            message: "长度在 6 到 20 个字符",
            trigger: "blur",
          },
        ],
        confirmPassword: [
          {required: true, message: "确认密码不能为空", trigger: "blur"},
          {required: true, validator: equalToPassword, trigger: "blur"},
        ],
      },
    };
  },
  methods: {
    handleDelete(row){
      delRoomOrder(row.id).then(res=>{
        Message.success("删除成功")
        this.getRoom()
      })
    },
    handleDelete2(row){
      delRestaurantOrder(row.id).then(res=>{
        Message.success("删除成功")
        this.getRestaurant()
      })
    },
    handleCancel(row){
      cancelRoomOrder(row.id).then(res=>{
        Message.success("取消成功")
        this.getRoom()
      })
    },
    handleCancel2(row){
      cancelRestaurantOrder(row.id).then(res=>{
        Message.success("取消成功")
        this.getRestaurant()
      })
    },
    handleSizeChange(val) {
      this.roomQuery.pageSize = val;
      this.getRoom();
    },
    handleCurrentChange(val) {
      this.roomQuery.pageNum = val;
      this.getRoom();
    },
    handleSizeChange2(val) {
      this.restaurantQuery.pageSize = val;
      this.getRestaurant();
    },
    handleCurrentChange2(val) {
      this.restaurantQuery.pageNum = val;
      this.getRestaurant();
    },
    getUser() {
      getTourist().then((res) => {
        if (res.code === 200) {
          this.user = res.data;
          this.pwdUser = res.data
          this.pwdUser.password = ""
        }
      });
    },
    getRoom() {
      getRoomOrder(this.roomQuery).then(res => {
        this.roomData=res.rows
        this.roomQuery.total=res.total
      })
    },
    getRestaurant() {
      getRestaurantOrder(this.restaurantQuery).then(res => {
        this.restaurantData=res.rows
        this.restaurantQuery.total=res.total

      })
    },
    submit() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          updateTourist(this.user).then((response) => {
            this.$message({
              message: "修改成功",
              type: "success",
            });
          });
        }
      });
    },
    submit2() {
      console.log(this.pwdUser)
      this.$refs["pwdForm"].validate((valid) => {

        if (valid) {
          updateTouristPwd(
              this.pwdUser
          ).then((response) => {
            this.$message({
              message: "修改成功",
              type: "success",
            });
          });
        }
      });
    },

  },
  created() {
    this.getUser();
    this.getRoom();
    this.getRestaurant();
  },
};
</script>

<style scoped lang="scss">
.text {
  font-size: 14px;
}

.container {
  height: 1000px;
  width: 100%;
  background-color: #f4f4f4;
  padding: 1em 0;
}

.wrapper {
  padding: 40px 0px 0px 200px;
}

.item {
  margin-bottom: 18px;
}

.page {
  padding-left: 30%;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both;
}

.box-card {
  width: 1200px;
  height: auto;
}

.el-tooltip__popper {
  max-width: 30% !important; //宽度可根据自己需要进行设置
}
</style>
